package com.mail.run;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.mail.bean.EmailInfo;
import com.mail.dao.IEmailInfoDao;
import com.mail.service.IEmailInfoService;

@Component
public class CheckLoginMail implements Runnable {

    private static ApplicationContext ctx = null;
    private static IEmailInfoService emailInfoService;
    private static CheckMail cm;
    private static IEmailInfoDao dao;
    public static void main(String[] args) {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        emailInfoService = ctx.getBean(IEmailInfoService.class);
        cm = ctx.getBean(CheckMail.class);
        dao = ctx.getBean(IEmailInfoDao.class);
        CheckLoginMail checkLoginMail = new CheckLoginMail();
        Thread d = new Thread(checkLoginMail, "12");
        d.start();
    }

    public void run() {
        List<EmailInfo> list = emailInfoService.queryList("@126.com");
        System.out.println("size:" + list.size());
        System.out.println("预计消耗时间:" + list.size() / 2);
        for (EmailInfo emailInfo : list) {
            System.out.println("check account:" + emailInfo.getAddress());
            if (cm.loginEmail(emailInfo.getAddress(), emailInfo.getPassword())) {
                dao.saveCanLoginAccount(emailInfo);
                System.out.println("ok account:" + emailInfo.getAddress());
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
